<?php

namespace App\Modules\Admin\Services\Mall;

use App\Exceptions\Exception;
use App\Modules\Admin\Entities\Mall\GoodsCateModel;
use App\Modules\Admin\Entities\Mall\GoodsModel;
use App\Modules\Admin\Entities\Mall\GoodsRuleModel;
use App\Modules\Admin\Entities\Mall\GoodsRuleTplModel;
use App\Modules\Admin\Entities\Water\WaterDeviceModel;
use App\Modules\Admin\Services\BaseService;
use Illuminate\Support\Facades\DB;

class GoodsRuleTplServices extends BaseService
{
    public function __construct(GoodsRuleTplModel $model)
    {
        $this->model = $model;
    }
    public function lists(array $params) : array
    {
        $model = $this->model;

        $lists = $model->paginate($this->getLimit($params['limit'] ?? 10));
        $list_data=[];
        foreach ($lists->items() as &$item) {
            $attr_name = $attr_value = [];
            if ($item['rule_value']) {

                $specs = json_decode($item['rule_value'], true);
                if ($specs) {
                    foreach ($specs as $key => $value) {
                        $attr_name[] = $value['value']  ?? '';
                        $attr_value[] = implode(',', $value['detail'] ?? '');
                    }
                } else {
                    $attr_name[] = '';
                    $attr_value[] = '';
                }
                $item['rule_value'] = $specs;
                $item['attr_name'] = implode(',', $attr_name);
                $item['attr_value'] = $attr_value;
            }
            $list_data[]=$item;
        }

        return [
            'per_page'     => $lists->perPage(),
            'count_page'   => $lists->lastPage(),
            'total'        => $lists->total(),
            'data'         => $list_data,
        ];

    }
    public function create(array $params)
    {

        DB::beginTransaction();
        try{
            $data['rule_name'] = $params['rule_name'];
            $data['rule_value'] = json_encode($params['spec']);
            parent::create($data); // TODO: Change the autogenerated stub
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }
    public function update(array $params)
    {
        DB::beginTransaction();
        try{
            $data['id'] = $params['id'];
            $data['rule_name'] = $params['rule_name'];
            $data['rule_value'] = json_encode($params['spec']);
            parent::update($data); // TODO: Change the autogenerated stub
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }
    public function detail($request)
    {
        $id = $request->all();
        $info = $this->model->where(['id'=>$id])->first();
        $info['spec'] = json_decode($info['rule_value'], true);
        return $info;
    }

    public function getSelectLists($request)
    {
        $lists = $this->model->get()->toArray();
        foreach ($lists as &$item) {
            $attr_name = $attr_value = [];
            if ($item['rule_value']) {
                $specs = json_decode($item['rule_value'], true);
                if ($specs) {
                    foreach ($specs as $key => $value) {
                        $attr_name[] = $value['value']  ?? '';
                        $attr_name[] = $value['value']  ?? '';
                        $attr_value[] = implode(',', $value['detail'] ?? '');
                    }
                } else {
                    $attr_name[] = '';
                    $attr_value[] = '';
                }
                $item['rule_value'] = $specs;
                $item['attr_name'] = implode(',', $attr_name);
                $item['attr_value'] = $attr_value;
            }
        }
        return $lists;
    }
}
